<?php
session_start();
require_once( 'user_db.php' );
 
$type = new M_user();
$user_id = $_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
   
      $result=$type->loadData();
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['username'],$row['password'],$row['name'],$row['departmentname'],$row['groupname'], $row['lastaccess'], $row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['username']=  $_POST['user'];
		$ar['password']=  $_POST['passwd'];
		$ar['name']=  $_POST['name'];
		$ar['gid']=  $_POST['group'];
		$ar['did']=  $_POST['department'];
		$ar['status'] = $_POST['s'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		 
		$id=$type->InsertData($ar);
		
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$ar['username']=  $_POST['user'];
		$ar['password']=  $_POST['passwd'];
		$ar['name']=  $_POST['name'];
		$ar['gid']=  $_POST['group'];
		$ar['did']=  $_POST['department'];
		$ar['status'] = $_POST['s'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		 
		$strcipt = $type->updateData($_POST['id'],$ar);
		// echo json_encode($strcipt);
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		 $type->deleData($id);
		 
    }/// Age Criteria
	else if ($_POST['m']=='update_profile'){
		$id=$_SESSION['userid'];
		$password = $_POST['password'];
		$name = $_POST['name'];
		$_SESSION['login_name'] = $_POST['name'];
		$type->updateProfile($id,$name,$password);
		echo 1;
	}
	else if ($_POST['m']=='update_profile_name'){
		$id=$_SESSION['userid'];
		$name = $_POST['name'];
		$_SESSION['login_name'] = $_POST['name'];
		$type->updateProfile_name($id,$name);
		echo 1;
	}	    
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if($_GET['m']=='gy')
			echo json_encode( $type->getUserGroup());
		else if($_GET['m']=='g')
			echo json_encode( $type->getUserGroup('Y'));
		else if ($_GET['m']=='d')
			echo json_encode( $type->getDepartment());
		else if ($_GET['m']=='dy')
			echo json_encode( $type->getDepartment('Y'));
	}
}
 
?>